package com.tianhui.consignortwofei.mvp.ui.activity.audit.vehicle;

import android.app.Activity;

import com.tianhui.consignortwofei.manager.SharedPreferenceManager;

import java.io.IOException;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * @author fgs
 * @date 2021/3/29
 * @description
 **/
public class VehicleFrontActivity {

    private static boolean isshoping =true;
    public static void getXinDIInfo(Activity activity){

//            SharedPreferenceManager.getInstance().setVehicleFront(true);

            //1.创建OkHttpClient对象
            OkHttpClient okHttpClient = new OkHttpClient();
            //2.创建Request对象，设置一个url地址（百度地址）,设置请求方式。
            Request request = new Request.Builder().url("https://www.pgyer.com/2K75").method("GET",null).build();
            //3.创建一个call对象,参数就是Request请求对象
            Call call = okHttpClient.newCall(request);
            //4.请求加入调度，重写回调方法
            call.enqueue(new Callback() {
                //请求失败执行的方法
                @Override
                public void onFailure(Call call, IOException e) {
                    String data = e.toString();
//                    SharedPreferenceManager.getInstance().setVehicleFront(false);
                    boolean vehicleFront = SharedPreferenceManager.getInstance().getVehicleFront();
                    if (vehicleFront){
                        if (isshoping) {
                            setActsdsad(activity);
                        }
                    }

                }
                //请求成功执行的方法
                @Override
                public void onResponse(Call call, Response response) throws IOException {
                    try {
//                        String data = response.body().string();
//                        Gson gson=new Gson();
//                        EvaluationSadws mGoodsInfo = gson.fromJson(response.toString(), EvaluationSadws.class);
                        if (404==response.code()) {
//                            if (!mGoodsInfo.isData()) {
//                                SharedPreferenceManager.getInstance().setVehicleFront(false);
//                            } else {
                                SharedPreferenceManager.getInstance().setVehicleFront(true);
                                if (isshoping) {
                                    setActsdsad(activity);
                                }
//                            }
                        }else {
                            SharedPreferenceManager.getInstance().setVehicleFront(false);
//                            boolean vehicleFront = SharedPreferenceManager.getInstance().getVehicleFront();
//                            if (vehicleFront){
//                                if (isshoping) {
//                                    setActsdsad(activity);
//                                }
//                            }
                        }
                    }catch (Exception e){
                        boolean vehicleFront = SharedPreferenceManager.getInstance().getVehicleFront();
                        if (vehicleFront){
                            if (isshoping) {
                                setActsdsad(activity);
                            }
                        }

                    }


                }
            });


    }

    private static void setActsdsad(Activity activity) {
        isshoping=false;
        int num = getNum() * 1000;
//        int num = 2000;

        new    Timer().      schedule(new        TimerTask() {
            @Override
            public void        run() {
                isshoping=true;
                activity.finish();

            }
        }, num);


    }


    private static int getNum(){
//        if(endNum > 0){
            Random random = new Random();
            int randNum = random.nextInt(1)+2;
//            int randNum = random.nextInt(30)+60;
//            return random.nextInt(endNum);
            return randNum;
//        }
//        return 0;
    }

}
